package com.orphan.marketBack.service.impl;

import com.orphan.marketBack.dao.GoodsMapper;
import com.orphan.marketBack.dao.GoodsShowMapper;
import com.orphan.marketBack.dao.InGoodsMapper;
import com.orphan.marketBack.dao.StockMapper;
import com.orphan.marketBack.po.Goods;
import com.orphan.marketBack.po.GoodsShow;
import com.orphan.marketBack.po.InGoods;
import com.orphan.marketBack.po.Stock;
import com.orphan.marketBack.service.InGoodsService;
import com.orphan.marketBack.util.DateUtil;
import com.orphan.marketBack.vo.ResultVO;
import com.orphan.util.dto.ListInfo;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class InGoodsServiceImpl implements InGoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsShowMapper goodsShowMapper;
    @Autowired
    private InGoodsMapper inGoodsMapper;
    @Autowired
    private StockMapper stockMapper;
    @Autowired
    RabbitTemplate rabbitTemplate;

    //获得供应商传过来的商品
    @RabbitListener(queues = "queueSupplier")
    public void getMessage(ListInfo goods){
        long l = System.currentTimeMillis();
        goods.setGoodsInTime(DateUtil.timeStamp2Date(String.valueOf(l),null));
        System.out.println(goods);
        InGoods inGoods = new InGoods();
        BeanUtils.copyProperties(goods,inGoods);
        boolean b = insert(inGoods);
        if(b){
            goods.setGoodsTag(2);
            rabbitTemplate.convertAndSend("directSupplierOut","supplierOut",goods);
        }
    }


    public boolean insert(InGoods inGoods) {
        boolean flag = false;
        Goods goods = new Goods();
        Stock stock = new Stock();
        GoodsShow goodsShow = new GoodsShow();
        //新增库存
        stock.setStockId(inGoods.getGoodsInId() + "KC101");
        stock.setGoodsName(inGoods.getGoodsName());
        stock.setStockNum(inGoods.getGoodsInNum());
        //记录商品信息
        goods.setGoodsPrice(inGoods.getGoodsPrice());
        goods.setGoodsType(inGoods.getGoodsType());
        goods.setStockId(inGoods.getGoodsInId() + "KC101");
        goods.setGoodsSize(inGoods.getGoodsSize());
        //修改入库商品的状态
        goodsShow.setStockId(inGoods.getGoodsInId() + "KC101");
        goodsShow.setGoodsShow("未上架");
        goodsShow.setGoodsPromotion(0);
        //添加到数据库中
        int i1 = inGoodsMapper.insert(inGoods);
        int i2 = goodsMapper.insert(goods);
        int i3 = goodsShowMapper.insert(goodsShow);
        int i4 = stockMapper.insert(stock);

        if(i1 > 0 && i2 > 0 && i3 > 0 && i4 > 0){
            flag = true;
        }
        return flag;
    }

    @Override
    public ResultVO getInGoods() {
        List<InGoods> inGoods = inGoodsMapper.getInGoods();
        ResultVO resultVO = new ResultVO();
        resultVO.setData(inGoods);
        resultVO.setTotal(inGoods.size());
        return resultVO;
    }
}
